package com.gotokeep.keep.composition.demo.samples;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.SurfaceTexture;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.TextureView;

import com.gotokeep.keep.composition.demo.R;
import com.gotokeep.keep.composition.demo.internal.GLProducerThread;
import com.gotokeep.keep.composition.demo.renderer.GLRendererImpl;
import com.gotokeep.keep.composition.demo.widgets.ScalableTextureView;

import java.util.concurrent.atomic.AtomicBoolean;

public class DrawActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener {

    private ScalableTextureView previewView;
    private Thread mProducerThread = null;
    private GLRendererImpl mRenderer;
    private AtomicBoolean render = new AtomicBoolean(true);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_draw);

        previewView = findViewById(R.id.preview_view);
        previewView.setSurfaceTextureListener(this);
        Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.drawable_resource);
        previewView.setVideoSize(b.getWidth(), b.getHeight(), 0);
        b.recycle();
        mRenderer = new GLRendererImpl(this);
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        mRenderer.setViewport(width, height);
        mProducerThread = new GLProducerThread(surface, mRenderer, render);
        mProducerThread.start();
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
        mRenderer.resize(width, height);
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        render.set(false);
        mProducerThread = null;
        return true;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {

    }
}
